package com.fbinggun.fattribute.util;

import com.alibaba.fastjson.JSONObject;
import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import com.fbinggun.fattribute.effect.IEffect;
import com.fbinggun.fattribute.status.StatusMap;
import org.bukkit.entity.LivingEntity;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class TempData {


    UUID uuid;
    public StatusMap statusMap;
    public Map<IEffect,Integer> iEffects = new HashMap<>();
    public TempData(EntityData entityData){
        uuid=entityData.getUuid();
        statusMap=entityData.getStatusMap();
        iEffects=entityData.getiEffects();
    }


    public JSONObject toJson(){
        JSONObject jsonObject= new JSONObject();
        JSONObject statJson = statusMap.toJson();
        if(statJson!=null) {
                jsonObject.put("StatusMap", statusMap.toJson());
        }
        jsonObject.put("UUID",uuid.toString());
        return jsonObject;
    }

    public TempData(JSONObject jsonObject){
        JSONObject statJson = jsonObject.getJSONObject("StatusMap");
        if(statJson==null){
            statusMap=new StatusMap();
        }else {
            statusMap=new StatusMap(statJson);
        }
        uuid=UUID.fromString(jsonObject.getString("UUID"));
    }


    public void loadEntityData(LivingEntity entity){
        EntityData entityData= new EntityData(entity);
        entityData.statusMap=statusMap;
        DataManage.putEntityData(entityData);
        try {
            FAttributePlugin.tempTable.delJson(uuid.toString());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


}
